﻿package com.mixmedia.net {
	import flash.display.DisplayObjectContainer;	import flash.display.MovieClip;	import flash.events.Event;	import flash.events.EventDispatcher;	import flash.events.IOErrorEvent;		import com.mixmedia.event.EventDispatcherUtils;	import com.mixmedia.utils.Queue;	import com.mixmedia.view.status.BindMovieClip;	
	/**
	 * @author colin
	 * provide the following service
	 * 1. loading Queue
	 * 2. error icon
	 * 3. preload icon
	 * 
	 * in default, error icon and preloader icon attach to _root
	 */
	final public class Loader extends EventDispatcher implements ILoader{
		private var base : ILoader;
	
		private var request : String;
		private var preloaderParent : DisplayObjectContainer; 
		private var preloaderID : String;
	
		private var errorIconParent : DisplayObjectContainer;
		private var errorIconID : String;
		
		private var queue:Queue;
	
		public function Loader(base:ILoader,queueThreadId:Number=-1){
			preloaderParent = AS2.instance().root;
			errorIconParent = preloaderParent;
	
			if(queueThreadId!=-1)queue = Queue.instance(queueThreadId);
	
			this.base = base;
			base.addEventListener(LoaderEvent.COMPLETE,forwardEvent);
			base.addEventListener(LoaderEvent.OPEN,    forwardEvent);
			base.addEventListener(LoaderEvent.PROGRESS,forwardEvent);
			base.addEventListener(LoaderEvent.READY,   forwardEvent);
			base.addEventListener(IOErrorEvent.IO_ERROR,forwardEvent);
			base.addEventListener(IOErrorEvent.IO_ERROR,onError);
			base.addEventListener(Event.COMPLETE,onLoadComplete);
		}
	
		public function load(request:String):void{
			this.request = request;
	
			if(queue==null){
				doLoad();
			}else{
				queue.addRequest(doLoad);
			}
		}
	
		public function getTargetContainer():Object{
			return base.getTargetContainer();
		}
	
		private function doLoad():void{
			makePreloader();
			base.load(request);
		}
	
		private function forwardEvent(evt:Event):void{
			dispatchEvent(evt);
		}
	
		private function makePreloader():void{
			BindMovieClip.make(BindMovieClip.PROGRESS, preloaderParent, preloaderID, {target:getTargetContainer()});
		}
	
		private function killPreloader():void{
			BindMovieClip.remove(BindMovieClip.PROGRESS, preloaderParent);
		}
		
		public function setPreloader(preloaderParent:MovieClip,symbolIdentifier:String=null):void{
			this.preloaderParent = preloaderParent;
			this.preloaderID = (symbolIdentifier==null)?preloaderID:symbolIdentifier;
		}
		
		public function setErrorIcon(errorIconParent:MovieClip,symbolIdentifier:String=null):void{
			this.errorIconParent = errorIconParent;
			this.errorIconID = (symbolIdentifier==null)?errorIconID:symbolIdentifier;
		}
	
		private function onLoadComplete() : void {
			queue.next();
			killPreloader();
		}
	
		private function onError():void{
			queue.next();
			BindMovieClip.make(BindMovieClip.ERROR,errorIconParent,errorIconID,{});
			killPreloader();
		}
		
		public function unload() : void {
			base.unload();
		}				public function when(eventType : String, whichObject : Object, callFunction : Function) : void {
			EventDispatcherUtils.when(this,eventType,whichObject,callFunction);
		}
	}
}